﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.MasterData
{
    public partial class frmMaintainAreaQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region Variable
        private MstAreaEntity mSelectedArea;
        public MstAreaEntity SelectedArea
        {
            get { return mSelectedArea; }
        }
        private MstAreaEntity mParentArea;
        private bool mPrivinceOnly = false;
        private string mAreaName;
        #endregion

        #region Constructor
        public frmMaintainAreaQuery()
        {
            InitializeComponent();
        }
        public frmMaintainAreaQuery(bool privinceOnly, string areaName)
        {
            InitializeComponent();
            mPrivinceOnly = privinceOnly;
            mAreaName = areaName;
        }
        public frmMaintainAreaQuery(bool privinceOnly, string areaName, MstAreaEntity parentArea)
        {
            InitializeComponent();
            mPrivinceOnly = privinceOnly;
            mAreaName = areaName;
            mParentArea = parentArea;
        }
        public frmMaintainAreaQuery(MstAreaEntity parentArea)
        {
            InitializeComponent();
            mParentArea = parentArea;
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByEnum(cmbAreaType, typeof(AreaType), typeof(string), true);

            UICommon.UICommonFunctions.BindGridComboboxByEnum(colAREA_TYPE, typeof(AreaType), typeof(string), true);

            List<MstAreaEntity> list = MstAreaBs.GetAllProvince();
            MstAreaEntity entity = new MstAreaEntity();
            list.Insert(0, entity);
            cmbParentArea.DataSource = list;
            cmbParentArea.DisplayMember = "AreaName";
            cmbParentArea.ValueMember = "AreaId";
        }

        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtAreaName.Text) ==false)
            {
                condition += " AND A.AREA_NAME like '%" + txtAreaName.Text + "%' ";
            }
            if (string.IsNullOrEmpty(cmbAreaType.Text) == false && cmbAreaType.SelectedValue != null)
            {
                condition += " AND A.AREA_TYPE = '" + cmbAreaType.SelectedValue.ToString() + "' ";
            }
            if (string.IsNullOrEmpty(cmbParentArea.Text) == false && cmbParentArea.SelectedValue != null)
            {
                condition += " AND A.PARENT_AREA_ID = '" + cmbParentArea.SelectedValue.ToString() + "' ";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "A.AREA_NAME";
            DataTable dt = MstAreaBs.GetAreaByCondition(condition, orderBy, "");
            gvMain.DataSource = dt;
        }

        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            BindComboBox();
            if (mParentArea != null)
            {
                cmbParentArea.SelectedValue = mParentArea.AreaId;
            }
            if (mPrivinceOnly)
            {
                cmbAreaType.SelectedValue = "1";
            }
            if (string.IsNullOrEmpty(mAreaName) == false)
            {
                txtAreaName.Text = mAreaName;
            }
            BindDataGrid();
            base.OnShown(e);
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Confirm()
        {
            if (mSelectedArea == null)
            {
                MessageBox.Show("请选择一个地区！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            this.DialogResult = DialogResult.OK;
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedArea = new MstAreaEntity(dr);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (mSelectedArea != null)
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        #endregion

    }
}
